package com.AAL.Action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import com.AAL.DAO.CityDAO;
import com.AAL.DAO.DepartmentDAO;
import com.AAL.DAO.EmployeeDAO;
import com.AAL.DAO.StatusOfAccountDAO;
import com.AAL.Model.Account;
import com.AAL.Model.AccountInformation;
import com.AAL.Model.City;
import com.AAL.Model.Department;
import com.AAL.Model.Employee;
import com.AAL.Model.StatusOfAccount;
import com.AAL.Utils.DateUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class EmployeeAction extends ActionSupport implements SessionAware {

	private Map<String, Object> session;
	//Phatdt start
	private Account account;
	private City city;
	private CityDAO cityDAO;
	private Department department;
	private DepartmentDAO departmentDAO;
	private EmployeeDAO employeeDAO;
	private StatusOfAccountDAO statusOfAccountDAO;
	private List<StatusOfAccount> listStatusOfAccounts;
	private List<City> listCity;
	private List<Employee> listEmployee;
	private List<Employee> listEmplSearch;
	private int id;
	private String fullname;
	private String depmtchose;
	private String statusChose;
	public List<StatusOfAccount> getListStatusOfAccounts() {
		return listStatusOfAccounts;
	}

	public void setListStatusOfAccounts(List<StatusOfAccount> listStatusOfAccounts) {
		this.listStatusOfAccounts = listStatusOfAccounts;
	}

	
	public String getStatusChose() {
		return statusChose;
	}

	public void setStatusChose(String statusChose) {
		this.statusChose = statusChose;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getDepmtchose() {
		return depmtchose;
	}

	public void setDepmtchose(String depmtchose) {
		this.depmtchose = depmtchose;
	}

	private Employee employee;
	

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public List<Employee> getListEmplSearch() {
		return listEmplSearch;
	}

	public void setListEmplSearch(List<Employee> listEmplSearch) {
		this.listEmplSearch = listEmplSearch;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public List<Employee> getListEmployee() {
		return listEmployee;
	}

	public void setListEmployee(List<Employee> listEmployee) {
		this.listEmployee = listEmployee;
	}

	private List<Department> listdepmt;

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public DepartmentDAO getDepartmentDAO() {
		return departmentDAO;
	}

	public void setDepartmentDAO(DepartmentDAO departmentDAO) {
		this.departmentDAO = departmentDAO;
	}

	public List<Department> getListdepmt() {
		return listdepmt;
	}

	public void setListdepmt(List<Department> listdepmt) {
		this.listdepmt = listdepmt;
	}
	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public CityDAO getCityDAO() {
		return cityDAO;
	}

	public void setCityDAO(CityDAO cityDAO) {
		this.cityDAO = cityDAO;
	}

	public List<City> getListCity() {
		return listCity;
	}

	public void setListCity(List<City> listCity) {
		this.listCity = listCity;
	}
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	public StatusOfAccountDAO getStatusOfAccountDAO() {
		return statusOfAccountDAO;
	}

	public void setStatusOfAccountDAO(StatusOfAccountDAO statusOfAccountDAO) {
		this.statusOfAccountDAO = statusOfAccountDAO;
	}
	public EmployeeAction() {
		// TODO Auto-generated constructor stub
		this.cityDAO = new CityDAO();
		this.departmentDAO = new DepartmentDAO();
		this.employeeDAO = new EmployeeDAO();
		this.statusOfAccountDAO = new StatusOfAccountDAO();
	}
	//Phatdt start	

	public String managerEmployee() {
		try {
		System.out.println("goi quan li nhan vien");

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String viewListEmployee() {
		try {
			setListStatusOfAccounts(statusOfAccountDAO.getAllStatusOfAccounts());
			setListEmployee(employeeDAO.getAllListEmployee());
			setListdepmt(departmentDAO.getAllLstDepartment());	

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String viewAddEmployee() {
		try {			
			setListCity(cityDAO.getAllLstCity());
			setListdepmt(departmentDAO.getAllLstDepartment());		

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String AddEmployee() {
		try {			
			Account accountT = this.account;
			accountT.setCreationDate(DateUtil.getToday());
			accountT.setPassword("123456");
			accountT.setStatusOfAccount(statusOfAccountDAO.getStatusOfAccount(2));
			AccountInformation accInforT = this.account.getAccountInformation();
			accInforT.setAccount(accountT);
			Employee employeeTemp= this.account.getEmployee();
			employeeTemp.setAccount(accountT);	
			/*System.out.println("getUserName:"+employeeTemp.getAccount().getUserName());
			System.out.println("getFullName:"+employeeTemp.getAccount().getAccountInformation().getFullName());
			System.out.println("getCreationDate:"+employeeTemp.getAccount().getCreationDate());
			System.out.println("getPassword:"+employeeTemp.getAccount().getPassword());
			System.out.println("getStatusOfAccountID:"+employeeTemp.getAccount().getStatusOfAccount().getStatusOfAccountID());
			System.out.println("getStatusName:"+employeeTemp.getAccount().getStatusOfAccount().getStatusName());
			System.out.println("getAccountID:"+employeeTemp.getAccount().getAccountID());
			System.out.println("getDepartmentID:"+employeeTemp.getDepartment().getDepartmentID());
			System.out.println("getCityName:"+employeeTemp.getAccount().getAccountInformation().getCity().getCityName());
			System.out.println("getCityID:"+employeeTemp.getAccount().getAccountInformation().getCity().getCityID());
			System.out.println("getAddress:"+employeeTemp.getAccount().getAccountInformation().getAddress());
			System.out.println("day111111111111111");*/
			employeeTemp= employeeDAO.saveAndUpdateEmployee(employeeTemp);
			if (employeeTemp == null) {
				addActionError("Lỗi: Không thể tạo tài khoản.");
				return ERROR;
			}		

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String searchEmployee() {
		try {			
			setListStatusOfAccounts(statusOfAccountDAO.getAllStatusOfAccounts());
			setListdepmt(departmentDAO.getAllLstDepartment());		
			System.out.println("depmtchose "+depmtchose+"fullname1:"+fullname+"statusChose: "+statusChose);			
			setListEmplSearch(employeeDAO.getAllListEmployeeBySearch(fullname, depmtchose, statusChose));
			if(getListEmplSearch().isEmpty()){
				System.out.println("ko");
				addActionError("Dữ liệu không phù hợp!");
				return SUCCESS;
			}
			else {System.out.println("co");
			addActionError("co");
			return SUCCESS;
			}
			

		} catch (Exception e) {
			return ERROR;
		}
		
	}

	public String viewProfileForEmployee() {
		try {
			System.out.println("goi view profile");
			employee = employeeDAO.getEmployee(id);

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String viewEditProfileForEmployee() {
		try {
			System.out.println("goi vvedit profile");
			System.out.println("id"+id);
			setListCity(cityDAO.getAllLstCity());
			setListStatusOfAccounts(statusOfAccountDAO.getAllStatusOfAccounts());
			setListdepmt(departmentDAO.getAllLstDepartment());		
			employee = employeeDAO.getEmployee(id);			
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String editProfileForEmployee() {
		try {
			System.out.println("goi edit profile"+id);
			System.out.println("day111111111111111");
			HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);			
			int EmployeeID= Integer.parseInt(request.getParameter("EmployeeID"));	
			Employee employeeTamp1 = this.employee;
			System.out.println("name:"+employeeTamp1.getAccount().getAccountInformation().getFullName());
			System.out.println("name:"+employeeTamp1.getAccount().getStatusOfAccount().getStatusName());
			System.out.println("name:"+employeeTamp1.getAccount().getAccountInformation().getAddress());
			System.out.println("getDepartmentName:"+employeeTamp1.getEmployeeID()+"a "+EmployeeID);
			System.out.println("stu:"+employeeTamp1.getAccount().getStatusOfAccount().getStatusOfAccountID());
			Employee employeeUpdate = employeeDAO.getEmployee(EmployeeID);
			employeeUpdate.getAccount().getAccountInformation().setFullName(employeeTamp1.getAccount().getAccountInformation().getFullName());
			employeeUpdate.getAccount().getAccountInformation().setBirthDate(employeeTamp1.getAccount().getAccountInformation().getBirthDate());
			employeeUpdate.getAccount().getAccountInformation().getCity().setCityID(employeeTamp1.getAccount().getAccountInformation().getCity().getCityID());
			employeeUpdate.getAccount().getAccountInformation().setAddress(employeeTamp1.getAccount().getAccountInformation().getAddress());
			employeeUpdate.getAccount().getAccountInformation().setEmail(employeeTamp1.getAccount().getAccountInformation().getEmail());
			employeeUpdate.getAccount().getAccountInformation().setHomePhone(employeeTamp1.getAccount().getAccountInformation().getHomePhone());
			employeeUpdate.getAccount().getAccountInformation().setTelephone(employeeTamp1.getAccount().getAccountInformation().getTelephone());
			employeeUpdate.getDepartment().setDepartmentID(employeeTamp1.getDepartment().getDepartmentID());
			employeeUpdate.getAccount().getStatusOfAccount().setStatusOfAccountID(employeeTamp1.getAccount().getStatusOfAccount().getStatusOfAccountID());
			System.out.println("pass:"+employeeUpdate.getAccount().getPassword());
			System.out.println("mail:"+employeeUpdate.getAccount().getAccountInformation().getEmail());
			/*employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));
			employeeUpdate.getAccount().getAccountInformation().setBirthDate(DateUtil.convertStringToTimestamp(request.getParameter("birthDate_update")));
			employeeUpdate.getAccount().getAccountInformation().getCity().setCityID((request.getParameter("cityID_update"));
			employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));
			employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));
			employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));
			employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));
			employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));
			employeeUpdate.getAccount().getAccountInformation().setFullName(request.getParameter("fullName_update"));*/			
			employeeUpdate= employeeDAO.saveAndUpdateEmployee(employeeUpdate);
			System.out.println(employeeUpdate.getAccount().getAccountInformation().getFullName());
			System.out.println(employeeUpdate.getAccount().getPassword());
			employee = employeeDAO.getEmployee(EmployeeID);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

   //PhatDT end

	public String changePassForEmployee() {
		try {
			System.out.println("goi change pass");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

}
